<?php
require_once(dirname(__FILE__) . '/config/index.php');

class TestOfArray extends UnitTestCase {

	protected $arr;

	public function setUp() {
		$this->arr = new Nina_Array(array('apple', 'carrot', 'banana'));
	}

	public function testGet() {
		$this->assertEqual($this->arr->get(), array('apple', 'carrot', 'banana'));
	}

	public function testContains() {
		$this->assertTrue($this->arr->contains('apple'));
		$this->assertFalse($this->arr->contains('date'));
	}
	
	public function testLength() {
		$this->assertEqual($this->arr->length(), 3);
	}
	
	public function testFirst() {
		$this->assertEqual($this->arr->first(), 'apple');
	}
	
	public function testLast() {
		$this->assertEqual($this->arr->last(), 'banana');
	}

	public function testPush() {
		$this->assertEqual($this->arr->push('date'), 4);
	}

	public function testPop() {
		$this->assertEqual($this->arr->pop(), 'banana');
	}

	public function testUnshift() {
		$this->assertEqual($this->arr->unshift('apple'), 4);
	}

	public function testShift() {
		$this->assertEqual($this->arr->shift(), 'apple');
	}
	
	public function testRandom() {
		$this->assertTrue(in_array($this->arr->random(), $this->arr->get()));
	}

	public function testSort() {
		$this->assertEqual($this->arr->sort(), array('apple', 'banana', 'carrot'));
		$this->assertEqual($this->arr->sort(false), array('carrot', 'banana', 'apple'));
	}

	public function testShuffle() {
		$arr = $this->arr->shuffle();
		foreach($this->arr->get() as $element) {
			$this->assertTrue(in_array($element, $arr));
		}
	}

	public function testDiff() {
		$this->assertEqual(array_values($this->arr->diff(array('apple', 'banana', 'date'))), array('carrot'));
	}

	public function testSelect() {
		$this->assertEqual(array_values($this->arr->select('TestOfArray::checkLength')), array('carrot', 'banana'));
	}

	public function testCollect() {
		$this->assertEqual($this->arr->collect('ucfirst'), array('Apple', 'Carrot', 'Banana'));
	}

	public function testMerge() {
		$this->assertEqual($this->arr->merge(array('date', 'egg')), array('apple', 'carrot', 'banana', 'date', 'egg'));
	}

	public function testUnique() {
		$this->arr->push('apple');
		$this->arr->push('banana');
		$this->assertEqual($this->arr->unique(), array('apple', 'carrot', 'banana'));
	}
	
	public function testDisplayJoin() {
		$this->assertEqual($this->arr->displayJoin(), 'apple, carrot and banana');
		$this->assertEqual($this->arr->displayJoin('or'), 'apple, carrot or banana');
		$this->assertEqual($this->arr->displayJoin('or', 'ucfirst'), 'Apple, Carrot or Banana');
		$this->assertEqual($this->arr->displayJoin('or', 'substr', 1), 'pple, arrot or anana');
	}
	
	public static function checkLength($arr) {
		return (strlen($arr) > 5);
	}
}
